package com.projectmanagement.helpers.i18n;

import java.io.File;

import com.vaadin.Application;

import eu.livotov.tpt.TPTApplication;

/**
 * This is class overriding default {@link eu.livotov.tpt.i18n.TM} behaviour, to
 * make sure dicitonaries are always initialized before accessing them via
 * {@link #get(Application, String, Object...)}<br/>
 * 
 * GAE Application <b>*must*</b> use this instead of the basic <b>tpt i18n TM</b>.
 * <br/>
 * Note that this way you don't have to extend {@link TPTApplication}, but the simple {@link Application} 
 */
public class TM {

	private static boolean translationsLoaded = false;

	public static String get(Application app, String key, Object... params) {
		if (!translationsLoaded)
			loadTranslations(app);
		return eu.livotov.tpt.i18n.TM.get(app, key, params);
	}

	private static void loadTranslations(Application app) {
		File themeFolder = new File(app.getContext().getBaseDirectory(),
				String.format("VAADIN/themes/%s", app.getTheme()));
		try {
			eu.livotov.tpt.i18n.TM.getDictionary(app)
					.loadTranslationFilesFromThemeFolder(themeFolder);
			translationsLoaded = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
